package Ajax;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;

public class AwardPage extends AbsPersonalPage {

	public AwardPage(String id, HttpServletRequest request) {
		super(id, request);
		mStrSubClassName = "resumeaward";
	}

	@Override
	public void execSql(Statement stmt, HttpServletRequest request)
			throws SQLException {
		if (bCreate) {
			return;
		}

		if (bSubmit) {

			String sql = "INSERT INTO award VALUES(null, " + mResumeId + ", "
					+ request.getParameter("awardYear") + ", "
					+ request.getParameter("awardMonth") + ", "
					+ (request.getParameter("property") == null ? "false" : "true") + ", '"
					+ request.getParameter("awardDesc") + "')";

			if (nModify > 0) {
				sql = "UPDATE award SET "
					+ "award_prop = " + (request.getParameter("property") == null ? "false" : "true")
					+ ", year = " + Integer.valueOf(request.getParameter("awardYear"))
					+ ", month = " + Integer.valueOf(request.getParameter("awardMonth"))
					+ ", award_desc = '" + request.getParameter("awardDesc")
					+ "' WHERE id = " + nModify;
			}

			stmt.executeUpdate(sql);

			return;
		}

		if (bDelete) {
			String sql = "DELETE FROM award WHERE id = " + mId;
			
			stmt.executeUpdate(sql);

			return;
		}

		String sql = "SELECT id, year, month, award_prop, award_desc FROM award WHERE ";

		if (nModify > 0) {
			sql += "id = " + nModify;
		} else {
			sql += "resume_id = " + mResumeId;
		}

		rs = stmt.executeQuery(sql);
	}

	@Override
	protected void writeWithoutCreate(PrintWriter writer) throws SQLException {
		if (rs == null) {
			return;
		}

		writer.print("<ol><li>属性: 公开, 您的简历可以被全部企业用户查看</li><li>属性: 隐藏, 企业用户不可以查看您的简历详情</li>"
				+ "</ol>"
				+ "<div class=\"topic_list\">"
				+ "<table cellpadding=\"0\" cellspacing=\"0\">"
				+ "<thead>"
				+ "<tr><td>属性</td><td>奖项时间</td><td>奖项描述</td><td>管理选项</td></tr>"
				+ "</thead>");

		int i = 0;

		while (rs.next()) {
			boolean is_public = rs.getBoolean("award_prop");

			writer.println("<tr class=\"bg" + (++i % 2) + "\">");
			writer.println("<td><img src=\"./images/personal/property_"
					+ (is_public ? "y" : "n") + ".gif\" alt=\"属性: "
					+ (is_public ? "公开" : "隐藏") + "\" /></td>");
			writer.println("<td>" + rs.getString("year") + "-" + rs.getString("month") + "</td>");
			writer.println("<td>" + rs.getString("award_desc") + "</td>");
			writer.println("<td><a href=\"#\" onclick=\"resumeaward('','','"
					+ rs.getString("id") + "');return false;\">修改</a> | ");
			writer.println("<a href=\"#\" onclick=\"if (confirm('您是否确定要删除该条教育经历\t')){resumeaward('','','','"
					+ rs.getString("id")
					+ "');return false;}else{return false;}\">删除</a></td>");
		}

		writer.println("</tr></table></div><table cellpadding=\"0\" cellspacing=\"0\" class=\"formtable\"><tr><th width=\"100\">&nbsp;</th>	<td>");
		writer.println("<input type=\"button\" name=\"create\" value=\"添加获奖情况\" onclick=\"resumeaward('',true)\" class=\"submit\" />");
		writer.println("<input type=\"button\" name=\"nextstep\" value=\"跳到下一步\" onclick=\"resumeload('assn')\" class=\"button\" />");
		writer.println("</td></tr></table><div id=\"resumextentform\"></div>");
	}

	@Override
	protected void writeWithCreate(PrintWriter writer) throws IOException {
		String form_title = "<form action=\"ResumeSys?action=personal&amp;option=resume&amp;extent=award&amp;info=" + mResumeId + "&amp;submit=1\" ";
		File file = new File("c:\\xml\\award.xml");

		if (file.exists()) {
			InputStream fs = new FileInputStream(file);
			BufferedReader br = new BufferedReader(new InputStreamReader(fs,
					"utf-8"));

			String Line = null;

			writer.print(form_title);

			while ((Line = br.readLine()) != null) {
				writer.println(Line);
			}

			br.close();
			fs.close();
		} else {
			file.createNewFile();
		}
	}

	@Override
	protected void writeModify(PrintWriter writer) throws SQLException {
		if (rs == null || !rs.next()) {
			return;
		}

		boolean is_public = rs.getBoolean("award_prop");

		writer.print("<form action=\"ResumeSys?action=personal&amp;option=resume&amp;extent=award&amp;info=" + mResumeId + "&amp;submit=1\" ");
		writer.print("name=\"wane_resume\" method=\"post\" target=\"ajaxform\" onsubmit=\"return resumeaward();\"><table cellpadding=\"0\" cellspacing=\"0\" class=\"formtable\" style=\"margin-bottom:30px;\">");
		writer.print("<caption><h2>修改获奖情况</h2></caption><tr><th width=\"100\"><span class=\"warn\">*</span> 奖项时间:</th><td>");
		
		// year
		writer.print("<select name=\"awardYear\"><option value=\"2012\" selected>");
		writer.print(rs.getInt("year"));
		writer.print("</option><option value=\"2011\" >2011</option><option value=\"2010\" >2010</option><option value=\"2009\" >2009</option><option value=\"2008\" >2008</option><option value=\"2007\" >2007</option><option value=\"2006\" >2006</option><option value=\"2005\" >2005</option><option value=\"2004\" >2004</option><option value=\"2003\" >2003</option><option value=\"2002\" >2002</option><option value=\"2001\" >2001</option><option value=\"2000\" >2000</option><option value=\"1999\" >1999</option><option value=\"1998\" >1998</option><option value=\"1997\" >1997</option><option value=\"1996\" >1996</option><option value=\"1995\" >1995</option><option value=\"1994\" >1994</option><option value=\"1993\" >1993</option></select> 年  ");
		writer.print("<select name=\"awardMonth\"><option value=\"1\" selected>");
		writer.print(rs.getInt("month"));
		writer.print("</option><option value=\"2\" >2</option><option value=\"3\" >3</option><option value=\"4\" >4</option><option value=\"5\" >5</option><option value=\"6\" >6</option><option value=\"7\" >7</option><option value=\"8\" >8</option><option value=\"9\" >9</option><option value=\"10\" >10</option><option value=\"11\" >11</option><option value=\"12\" >12</option></select> 月</td></tr>");

		writer.print("<tr><th><span class=\"warn\">*</span> 获奖描述:</th><td><textarea name=\"awardDesc\" cols=\"85\" rows=\"6\">" + rs.getString("award_desc") + "</textarea></td></tr>");
		writer.print("<tr><th><span class=\"warn\">*</span> 属性:</th><td>");
		writer.print("<input type=\"checkbox\" name=\"property\" value=\"1\" ");

		if (is_public) {
			writer.print("checked");
		}

		writer.print(" /> 属性: 公开</td></tr><tr>");

		writer.print("<input type=\"hidden\" name=\"modifysubmit\" value=\"1\" />");
		writer.print("<input type=\"hidden\" name=\"modifid\" value=\"" + nModify + "\">");
		writer.print("<th>&nbsp;</th><td><input type=\"submit\" name=\"Submit\" value=\"保存修改\" class=\"submit\" /><input type=\"button\" name=\"ClickBack\" value=\"取消操作\" onclick=\"resumeload('iter');\" class=\"button\" /></td></tr></table></form>");
	}
}
